<template>
    <div class="continer">
        <div class="buttom header-buttom">
      <router-link to="/shouye">
        <img
          src="./images/159642507148437980.png"
          alt="logo"
          class="sn-logo"
        />
      </router-link>
      <form class="search" @submit.prevent="toSearch">
        <input type="text" v-model="keyword" />
        <button>搜索</button>
      </form>
    </div>
    </div>
</template>

<script>
    export default {
        name:"ZSearch",
        data() {
    return {
      keyword: "",
    };
  },
  methods: {
    toSearch() {
      const keyword = this.keyword;
      const query = {
        ...this.$route.query,
      };
      delete query.keyword;
      if (keyword) {
        // 如果keyword存在，直接添加到query对象，没有只会添加一个空属性到query对象
        query.keyword = keyword;
      }
      this.$router.push({
        name: "Search",
        query,
      });
    },
  },
  watch: {
    $route: {
      handler(route) {
        this.keyword = route.query.keyword;
      },
      immediate: true,
    },
  },
    }
</script>

<style lang="less" scoped>

.continer{
  .buttom {
  width: 1200px;
  height: 100%;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
    
    .header-buttom {
  height: 100px;
}
.header-buttom .sn-logo {
  width: 190px;
  height: 90px;
}
.search {
  display: flex;
  justify-content: center;
  align-items: center;
}
.search input {
  height: 36px;
  width: 390px;
  border: 2px solid #ff8000;
  outline: none;
  padding: 0 5px;
  border-top-left-radius: 12px;
  border-bottom-left-radius: 12px;
}
.search button {
  border: none;
  height: 40px;
  width: 120px;
  color: #fff;
  font-size: 18px;
  font-weight: 800;
  background-color: #ff8000;
  border-top-right-radius: 12px;
  border-bottom-right-radius: 12px;
}
}
</style>